﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using System.Web.Security;
using Cake_Management.Models;

namespace Cake_Management.Controllers
{
    public class HT_AccountController : Controller
    {

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModels model)
        {
            int id = new BLL.B_Account().Login(model.Account1, model.Password);
            if (id > 0)
            {
                //设置登录用的Cookie       
                FormsAuthentication.SetAuthCookie(id.ToString(), model.Remember);
                return RedirectToAction("Login", "HT_Account");
            }
            return View();
        }

        // GET: HT_Account
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(Account account)
        {
            account.LastTiem = DateTime.Now;
            new BLL.B_Account().Add(account);
            return Json(new { Code = 200, Message = "添加成功" });
        }



        public ActionResult GetAll()
        {
            var result = new BLL.B_Account().GetAll();
            var sj = from a in result
                     select new
                     {
                         a.Id,
                         a.Account1,
                         Password = "******",
                         IsEnable = a.IsEnable.Value ? "已启用" : "禁用",
                         LastTime=a.LastTiem.ToString(),
                         Operation = $"<div data-id='{a.Id}' class='btn btn-warning data-edit' onclick='Edit(this)'>编辑</div>      <div data-id='{a.Id}' class='btn btn-danger data-del'onclick='Del(this)'>删除</div>"

                     };
            var data = new { data = sj };
            return Json(data, JsonRequestBehavior.AllowGet);
        }


        [HttpGet]
        // GET: HT_Account/Edit/5
        public ActionResult Edit(int id)
        {
            var data = new BLL.B_Account().FindById(id);
            return PartialView(data);
        }

        // POST: HT_Account/Edit/5
        [HttpPost]
        public ActionResult Edit(Account account)
        {
            account.LastTiem= DateTime.Now;
            new BLL.B_Account().Updata(account.Id, account);
            return Json(new { Code = 200, Message = "修改成功" });
        }

        // GET: HT_Account/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                new BLL.B_Account().Delete(id);
                return Json(new { Code = 200, Message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { Code = 500, Message = ex.Message });
            }
        }

    }
}
